<?php
require("libraries/agency.php");
class Checkrate extends Agency{
	protected $_user = "";
	public function __construct(){
		parent::__construct();
		$this->load->model("mcabinprice");
		$this->load->model("mcabins");
		$this->load->model("model_sheep");
		$this->_user = $this->session->userdata('ses_userid');
	}
	public function index(){
		$data["data"]=array();
		$this->load->view("layout",$data);
	}
	public function updatefield(){
		if($_POST["v"]!="0"){
			if(empty($_POST["v"])){
				echo "value is empty";
				return;
			}
		}
		$currentdata=$this->mcabinprice->getAllByDateAndCabinCode($_POST["d"],$_POST["sc"],$_POST["n"]);
		if($currentdata["id"]==0){
			$currentdata[$_POST["f"]]=$_POST["v"];
			$currentdata["cabinid"]=$_POST["sc"];
			$currentdata["pricedate"]=$_POST["d"];
			$currentdata["numbernight"]=$_POST["n"];
			$this->mcabinprice->add($currentdata);
			echo "ok";
			return;
		}else{
			$currentdata[$_POST["f"]]=$_POST["v"];
			$this->mcabinprice->update($currentdata);
			echo "ok";
			return;
		}
	}
	public function updateprice(){
		$data['act'] = 4;
		$data["data"]=array();
		$data["title"]="Update price of cruises";
		$data['data']['listsheep']=$this->model_sheep->getallName_Id();
		$data["data"]["listcruis"] = $this->mcabins->listcruises($this->_user);
		if(isset($_GET['sheepid'])){
			$sheepid=intval($_GET['sheepid']);
			$data["data"]["listcabin"]=$this->mcabins->getAllByShipid($sheepid);
		}else $data["data"]["listcabin"]=$this->mcabins->listall();
		$data["data"]["cabinid"]=0;
		if(isset($_POST["ok"])){
			$data["data"]["cabinid"]=$_POST["cabinid"];
			if(isset($_POST["onenight"])){
				for($i=0;$i<count($_POST["onenight"]["fromdate"]);$i++){
					$curdate=strtotime($_POST["onenight"]["fromdate"][$i]);
					while($curdate<=strtotime($_POST["onenight"]["todate"][$i])){
						$insertdata=array();
						$insertdata["pricedate"]=date("Y-m-d",$curdate);
						$insertdata["priceold"]=$_POST["onenight"]["priceold"][$i];
						$insertdata["pricedouble"]=$_POST["onenight"]["pricedouble"][$i];
						$insertdata["pricetwin"]=$_POST["onenight"]["pricetwin"][$i];
						$insertdata["pricesingle"]=$_POST["onenight"]["pricesingle"][$i];
						$insertdata["pricechild"]=$_POST["onenight"]["pricechild"][$i];
						$insertdata["priceinfant"]=$_POST["onenight"]["priceinfant"][$i];
						$insertdata["numberavai"]=$_POST["onenight"]["numberavai"][$i];
						$insertdata["numbernight"]= 1;
						$insertdata["cabinid"]=$_POST["cabinid"];
						$this->mcabinprice->updatesmart($insertdata["pricedate"],$insertdata["numbernight"],$insertdata["cabinid"],$insertdata);
						$curdate+=86400;
					}
				}
			}
			if(isset($_POST["charter"])){
				for($i=0;$i<count($_POST["twonight"]["fromdate"]);$i++){
					$curdate=strtotime($_POST["twonight"]["fromdate"][$i]);
					while($curdate<=strtotime($_POST["twonight"]["todate"][$i])){
						$insertdata=array();
						$insertdata["pricedate"]=date("Y-m-d",$curdate);
						$insertdata["priceold"]=$_POST["charter"]["priceold"][$i];
						$insertdata["pricedouble"]=$_POST["charter"]["pricedouble"][$i];
						$insertdata["pricetwin"]=$_POST["charter"]["pricetwin"][$i];
						$insertdata["pricesingle"]=$_POST["charter"]["pricesingle"][$i];
						$insertdata["pricechild"]=$_POST["charter"]["pricechild"][$i];
						$insertdata["priceinfant"]=$_POST["charter"]["priceinfant"][$i];
						$insertdata["numberavai"]=$_POST["charter"]["numberavai"][$i];
						$insertdata["numbernight"]=2;
						$insertdata["cabinid"]=$_POST["cabinid"];
						$this->mcabinprice->updatesmart($insertdata["pricedate"],$insertdata["numbernight"],$insertdata["cabinid"],$insertdata);
						$curdate+=86400;
					}
				}
			}
		}
		$data["template"]="checkrate/updateprice";
		$this->load->view("layout",$data);
	}
	function pricemanager(){
		$data['act'] = 4;
		$data["data"]=array();
		$data["title"]="Update price of cruises";
		$data["data"]["listcabin"]=$this->mcabins->listall();
		$data["data"]["numbernight"]=1;
		$data["data"]["cabinid"]=0;
		$data["data"]["listdata"]=array();
		$data["data"]["listcruis"] = $this->mcabins->listcruises($this->_user);
		//$this->debug($data["data"]["listcruis"]);
		if(isset($_POST["ok"])){
			$numbernight=isset($_POST["numbernight"])?$_POST["numbernight"]:1;
			$fromdate=strtotime($_POST["fromdate"]);
			$listdata=array();
			while($fromdate<=strtotime($_POST["todate"])){
				$listdata[date("Y-m-d",$fromdate)]=$this->mcabinprice->getAllByDateAndCabinCode(date("Y-m-d",$fromdate),$_POST["cabinid"],$numbernight);
				
				$fromdate+=3600*24;
			}
			$data["data"]["cabinid"]=$_POST["cabinid"];
			$data["data"]["listdata"]=$listdata;
			$data["data"]["numbernight"]=$_POST["numbernight"];
		}
		$data["template"]="checkrate/pricemanager";
		$this->load->view("layout",$data);
		
	}
	function opencloseroom(){
		$data['act'] = 4;
		$data["data"]=array();
		$data["data"]["listcabin"]=$this->mcabins->listall();
		if(isset($_POST["ok"])){
			$cabinid="''";
			if(isset($_POST["listcabin"])){
				foreach($_POST["listcabin"] as $c){
					$cabinid.=",'$c'";
				}
			}
			$numbernight="''";
			if(isset($_POST["listnumbernight"])){
				foreach($_POST["listnumbernight"] as $n){
					$numbernight.=",'$n'";
				}
			}
			$date="'','".str_replace(",","','",$_POST["date"])."'";
			if(is_numeric($_POST["numbercabin"])){
				$this->mcabinprice->updatenumberavaiablebyquery($cabinid,$numbernight,$date,intval($_POST["numbercabin"]));
				$message="Update number room okie.";
			}else{
				$message="number cabin is not number";
			}
		}
		$data["template"]="checkrate/opencloseroom";
		$data["data"]["message"]="";
		$this->load->view("layout",$data);
	}
	public function getrooms(){
		$ship_id = $this->input->post("val");
		$data    = $this->mcabinprice->getcabins($ship_id);
		echo "Select cabin<select name='cabinid' id='cabinid'>";
		echo "<option value='0'>Select room</option>";
		if($data != NULL){
			foreach($data as $items){
				echo "<option value='".$items['room_id']."'>".$items['room_title']."</option>";
			}
		}
		echo "</select>";
	}
}
?>